#to-be written
'''
We use ProcessorManager to create and manage processors
The customized processor should use this class to visit anole's other parts
So we can seperate customized processor from anole
'''
from anole.core.processor import ContentProcessor as C
from anole.core.processor import HeadProcessor as H
from anole.core.processor import CmdProcessor as CMD 
from anole.core.processor import ErrorProcessor as ERROR 
from anole import AnoleError,AnoleLog
class EAccessLimit(AnoleError):
        def __init__(self,part):
                str='access '+part+' is not permitted'
                self.args=str
class ProcessorMgr:
        #dynamic load all processors
	#and the  other anole component's interface to processor
        def __init__(self,parts,modules=[]):
		self._log=AnoleLog().log
                self.__access=parts
		self.__modules=modules
		self.__processors={}

        def get(self,name):
                try:
                        component=self.__access[name]
                except KeyError:
                        raise EAccessLimit(name)
                else:
                        return component
        def get_usp(self):
        #this method is for compatiable need
                return self.get('USP')
        def get_config(self):
        #this method is for compatiable need
                return self.get('CONFIG')

	def get_processors(self):
		pass

	def load_reg_ps(self,app):
		 ps=self.load_head_ps()
		 app.reg_head_ps(ps)
		 ps=self.load_content_ps()
		 #print ps
		 app.reg_content_ps(ps)
		 ps=self.load_error_ps()
		 app.reg_error_ps(ps)
		 ps=self.load_cmd_ps()
		 app.reg_cmd_ps(ps)

	def load_cmd_ps(self):
		ps=[]
		for moduleName in self.__modules:
			self._log('try load cmd processor from module %s'%moduleName,AnoleLog.LEVEL_INFO)
			module=__import__(moduleName,globals(),locals(),[moduleName])
                        for objname in dir(module):
                                obj=getattr(module,objname)
                                try:
					if obj==CMD:continue
					if not issubclass(obj,CMD): 
						continue
					self._log('load cmd processor %s'%objname,AnoleLog.LEVEL_INFO)
					if  self.__processors.has_key(obj):
						ps.append(self.__processors[obj])
					else:	
						o=obj(self)
						self.__processors[obj]=o
						ps.append(o)

                                except TypeError:
                                        pass
                return ps 
        def load_head_ps(self):
		ps=[]
                for moduleName in self.__modules:
			self._log('try load head processor from module %s'%moduleName,AnoleLog.LEVEL_INFO)
			module=__import__(moduleName,globals(),locals(),[moduleName])
                        for objname in dir(module):
                                obj=getattr(module,objname)
                                try:
					if obj==H:continue
					if not issubclass(obj,H): 
						continue
					self._log('load head processor %s'%objname,AnoleLog.LEVEL_INFO)
					if  self.__processors.has_key(obj):
						ps.append(self.__processors[obj])
					else:	
						o=obj(self)
						self.__processors[obj]=o
						ps.append(o)

                                except TypeError:
                                        pass
                return ps 
        def load_content_ps(self):
		ps=[]
		for moduleName in self.__modules:
			self._log('try load content processor from module %s'%moduleName,AnoleLog.LEVEL_INFO)
			module=__import__(moduleName,globals(),locals(),[moduleName])
			for objname in dir(module):
				obj=getattr(module,objname)
                                try:
					if obj==C:continue
					if not issubclass(obj,C): 
						continue
					self._log('load content processor %s'%objname,AnoleLog.LEVEL_INFO)
					if  self.__processors.has_key(obj):
						ps.append(self.__processors[obj])
					else:	
						o=obj(self)
						self.__processors[obj]=o
						ps.append(o)
					
				except TypeError:
					pass
                return ps 
	def load_error_ps(self):
		ps=[]
		for moduleName in self.__modules:
			self._log('try load error processor from module %s'%moduleName,AnoleLog.LEVEL_INFO)
			module=__import__(moduleName,globals(),locals(),[moduleName])
			for objname in dir(module):
				obj=getattr(module,objname)
                                try:
					if obj==ERROR:continue
					if not issubclass(obj,ERROR): 
						continue
					self._log('load error processor %s'%objname,AnoleLog.LEVEL_INFO)
					if  self.__processors.has_key(obj):
						ps.append(self.__processors[obj])
					else:	
						o=obj(self)
						self.__processors[obj]=o
						ps.append(o)
					
				except TypeError:
					pass
                return ps 
		

